'use strict';

define([
	'underscore',
	'backbone',
	'localStorage',
	'validation',
	'UrlConfig'
],function(
	_,
	Backbone,
	localStorage,
	validation,
	UrlConfig
){
	
	var supplierModel=Backbone.Model.extend({
		
		test:{
			id:'Personnel',
			store:new Store('personnel')
		},
		
		idAttribute:'Code',
		
		url:UrlConfig.basePersonnel,
		
		initialize:function(){
			
			//提交前验证绑定
			this.on("invalid",function(model,error){
				alert(error);
			});	

		},
		
		validate:function(attrs){
			if(_.isEmpty(attrs.Code)) return '供应商ID不能为空!';
		},
		
		createData:function(callback){
			var httpMethod='POST';
			var url=UrlConfig.addSupplier();
			var content=_.pick(this.toJSON(),'Code','Name','Location','Contact','Tel','Description');
			
			var options=this.getOptions(httpMethod,url,callback,content);
			
			if(!this.preValidate(this,options)) return false;
			//表单提交前验证成功后提交
			this.sync('create',this,options);
		},
		
		updateData:function(callback){
			var httpMethod='POST';
			var url=UrlConfig.updateSupplier();
			var content=_.pick(this.toJSON(),'Code','Name','Location','Contact','Tel','Description');
			
			var options=this.getOptions(httpMethod,url,callback,content);
			
			 if(!this.preValidate(this,options)) return false;
			//表单提交前验证成功后提交
			this.sync('update',this,options);
		},
		
		deleteData:function(id,callback){debugger
			var httpMethod='GET';
			var url=UrlConfig.deleteSupplier(id);
			
			var options=this.getOptions(httpMethod,url,callback);
			
			this.sync('delete',this,options);
		},
		
		getData:function(id,callback){
			this.set("Code",id);
			
			var httpMethod='GET';
			var url=UrlConfig.getSupplier(id);
			
			var options=this.getOptions(httpMethod,url,callback);
			
			this.fetchSrv(options);
		}
		
	});
	
	return supplierModel;
	
});
